package com.example.protestgps;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;

public class HttpPostgetUserID extends Activity {

	String user;
	String mobileno;

	GetID user_id = new GetID();

	public void receivedata(String user, String mobileno) {
		this.user = user;
		this.mobileno = mobileno;
	}

	
	public class GetID extends AsyncTask<String, String, JSONObject> {
		JSONObject result;
		 String json = "";

		@Override
		protected JSONObject doInBackground(String... urls) {
			ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();
			data.add(new BasicNameValuePair("user", ""+user));
			Log.d("user1",""+user);
			data.add(new BasicNameValuePair("mobileno", ""+mobileno));
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new  HttpPost(urls[0]);
			
			try {
				post.setEntity(new UrlEncodedFormEntity(data,HTTP.UTF_8));
				HttpResponse response = client.execute(post);
				HttpEntity entity = response.getEntity();
				
				if(entity != null){
					InputStream inputStream = entity.getContent();
					BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
					StringBuilder sb = new StringBuilder();
					String line = null;
					
					while((line = reader.readLine()) != null){
						sb.append(line + "\n");
					}
					
					inputStream.close();
					json = sb.toString();
					result = new JSONObject(json);
					System.out.println("result: "+result);		
				}				
				
			}catch (Exception e) {
				e.printStackTrace();
			}
			return result;

		}	

	}

}
